package org.grow.fund.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.grow.common.exception.BillStateException;
import org.grow.common.exception.MissTargetException;
import org.grow.common.interceptor.AuthInterceptor;
import org.grow.fund.domain.LoanBill;
import org.grow.fund.domain.LoanBillState;
import org.grow.fund.dto.LoanBillDto;
import org.grow.fund.mapper.LoanBillMapper;
import org.grow.fund.service.CodeGenerator;
import org.grow.fund.service.LoanBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2023/9/26
 */

@RestController
@RequestMapping("/loanBill")
public class LoanBillController {

    @Autowired
    private CodeGenerator codeGenerator;
    @Autowired
    private LoanBillService loanBillService;
    @Autowired
    private LoanBillMapper loadBillMapper;

    @PostMapping("/insert")
    public LoanBill insert(@RequestBody LoanBill loanBill) {
        LoanBill pureBill = new LoanBill();
        pureBill.setMaterialCode(loanBill.getMaterialCode());
        pureBill.setBizCard(loanBill.getBizCard());
        pureBill.setBizLicense(loanBill.getBizLicense());
        pureBill.setApplyAmount(loanBill.getApplyAmount());
        pureBill.setLoanBank(loanBill.getLoanBank());

        pureBill.setUpdateTime(LocalDateTime.now());
        pureBill.setLoanCode(codeGenerator.nextCode("LN"));
        pureBill.setBillState(LoanBillState.toSubmit);
        String currentUser = AuthInterceptor.getCurrentUser();
        pureBill.setLoanApplier(currentUser);
        loanBillService.save(pureBill);
        return pureBill;
    }

    @PutMapping("/modify")
    public LoanBill modify(@RequestBody LoanBill loanBill) {
        String loanCode = loanBill.getLoanCode();
        LoanBill loanBillFromDB = loanBillService.getById(loanCode);
        if (loanBillFromDB == null) {
            throw new MissTargetException("LoanBill is missing " + loanCode);
        }
        if (loanBillFromDB.getBillState() != LoanBillState.toSubmit) {
            throw new BillStateException("only bill_state is toSubmit can modify");
        }
        loanBillFromDB.setBillState(LoanBillState.toSubmit);
        loanBillFromDB.setBizCard(loanBill.getBizCard());
        loanBillFromDB.setBizLicense(loanBill.getBizLicense());
        loanBillFromDB.setApplyAmount(loanBill.getApplyAmount());
        loanBillFromDB.setUpdateTime(LocalDateTime.now());
        loanBillService.updateById(loanBillFromDB);
        return loanBillFromDB;
    }

    @GetMapping("/find/{code}")
    public LoanBill find(@PathVariable("code") String code) {
        LoanBill loanBill = loanBillService.getById(code);
        if (loanBill == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        return loanBill;
    }

    @DeleteMapping("/remove/{code}")
    public String remove(@PathVariable("code") String code) {
        LoanBill loanBill = loanBillService.getById(code);
        if (loanBill == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        if (loanBill.getBillState() != LoanBillState.toSubmit) {
            throw new BillStateException(" only state is to submit can be removed");
        }
        boolean b = loanBillService.removeById(code);
        if (!b) {
            throw new MissTargetException("removing is failed " + code);
        }

        return code;
    }

    @PutMapping("/submit/{code}")
    public LoanBill submit(@PathVariable("code") String code) {
        LoanBill loanBill = loanBillService.getById(code);
        if (loanBill == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        if (loanBill.getBillState() != LoanBillState.toSubmit) {
            throw new BillStateException(" only state is to submit can be submitted");
        }
        loanBill.setBillState(LoanBillState.toFirstAudit);
        loanBill.setUpdateTime(LocalDateTime.now());
        loanBillService.updateById(loanBill);
        return loanBill;
    }

    @PutMapping("/auditFirst")
    public LoanBill auditFirst(@RequestBody LoanBill loanBill) {
        String code = loanBill.getLoanCode();
        LoanBill loanBillFromDb = loanBillService.getById(code);
        if (loanBillFromDb == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        if (loanBillFromDb.getBillState() != LoanBillState.toFirstAudit) {
            throw new BillStateException(" only state is toFirstAudit can be firstAudit");
        }
        loanBillFromDb.setBillState(LoanBillState.toSecondAudit);
        loanBillFromDb.setFirstAuditor(AuthInterceptor.getCurrentUser());
        loanBillFromDb.setFirstAuditTime(LocalDateTime.now());
        loanBillFromDb.setFirstAuditWord(loanBill.getFirstAuditWord());
        loanBillFromDb.setUpdateTime(LocalDateTime.now());
        loanBillService.updateById(loanBillFromDb);
        return loanBillFromDb;
    }

    @PutMapping("/resent")
    public LoanBill resent(@RequestBody LoanBill loanBill) {
        String code = loanBill.getLoanCode();
        LoanBill loanBillFromDb = loanBillService.getById(code);
        if (loanBillFromDb == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        if (loanBillFromDb.getBillState() != LoanBillState.toFirstAudit) {
            throw new BillStateException(" only state is toFirstAudit can be resent");
        }
        loanBillFromDb.setBillState(LoanBillState.toSubmit);
        loanBillFromDb.setFirstAuditor(AuthInterceptor.getCurrentUser());
        loanBillFromDb.setFirstAuditTime(LocalDateTime.now());
        loanBillFromDb.setFirstAuditWord(loanBill.getFirstAuditWord());
        loanBillFromDb.setUpdateTime(LocalDateTime.now());
        loanBillService.updateById(loanBillFromDb);
        return loanBillFromDb;
    }

    @PutMapping("/auditSecond/{score}")
    public LoanBill auditSecond(@PathVariable("score") Integer score, @RequestBody LoanBill loanBill) {
        String code = loanBill.getLoanCode();
        LoanBill loanBillFromDb = loanBillService.getById(code);
        if (loanBillFromDb == null) {
            throw new MissTargetException("LoanBill is missing " + code);
        }
        if (loanBillFromDb.getBillState() != LoanBillState.toSecondAudit) {
            throw new BillStateException(" only state is toFirstAudit can be firstAudit");
        }
        if (!AuthInterceptor.getCurrentUser().equals(loanBillFromDb.getLoanBank())) {
            throw new MissTargetException("only self bank can audit ");
        }
        if (score > 0) {
            loanBillFromDb.setBillState(LoanBillState.passed);
        } else {
            loanBillFromDb.setBillState(LoanBillState.failed);
        }
        loanBillFromDb.setSecondAuditTime(LocalDateTime.now());

        loanBillFromDb.setSecondAuditWord(loanBill.getSecondAuditWord());
        loanBillFromDb.setUpdateTime(LocalDateTime.now());
        loanBillService.updateById(loanBillFromDb);
        return loanBillFromDb;
    }

//         updatetime
//    开发多条件查询
//    分别为 贷款公司  平台工作人员  银行工作人员
    @PostMapping("/queryForCompany")
    public IPage<LoanBill> queryForCompany(@RequestBody LoanBillDto loanBillDto) {
        String currentUser = AuthInterceptor.getCurrentUser();
        loanBillDto.setLoanApplier(currentUser);
        Page<LoanBill> page = Page.of(loanBillDto.getPageNum(), loanBillDto.getPageSize());
        return loadBillMapper.dynamicQueryForCompany(page, loanBillDto);
    }
    @PostMapping("/queryForStaff")
    public IPage<LoanBill> queryForStaff(@RequestBody LoanBillDto loanBillDto){
        String currentUser = AuthInterceptor.getCurrentUser();
        loanBillDto.setFirstAuditor(currentUser);
        Page<LoanBill> page = Page.of(loanBillDto.getPageNum(), loanBillDto.getPageSize());
        return loadBillMapper.dynamicQueryForStaff(page, loanBillDto);
    }
    @PostMapping("/queryForBank")
    public IPage<LoanBill> queryForBank(@RequestBody LoanBillDto loanBillDto){
        String currentUser = AuthInterceptor.getCurrentUser();
        loanBillDto.setBankName(currentUser);
        Page<LoanBill> page = Page.of(loanBillDto.getPageNum(), loanBillDto.getPageSize());
        return loadBillMapper.dynamicQueryForBank(page, loanBillDto);
    }
}
